<!--
  Copyright (c) 2006-2013, JGraph Ltd
  
  Backend example for mxGraph. This example demonstrates using
  AJAX to open and save XML from a Java backend.
-->
<html>
<head>
	<title>Backend example for mxGraph</title>

	<!-- Sets the basepath for the library if not in same directory -->
	<script type="text/javascript">
		mxBasePath = '/mxgraph/javascript/src';
	</script>

	<!-- Loads and initializes the library -->
	<script type="text/javascript" src="/mxgraph/javascript/src/js/mxClient.js"></script>

	<!-- Example code -->
	<script type="text/javascript">
		// Program starts here. Creates a sample graph in the
		// DOM node with the specified ID. This function is invoked
		// from the onLoad event handler of the document (see below).
		function main(container)
		{
			// Checks if the browser is supported
			if (!mxClient.isBrowserSupported())
			{
				// Displays an error message if the browser is not supported.
				mxUtils.error('Browser is not supported!', 200, false);
			}
			else
			{
				// Disables the built-in context menu
				mxEvent.disableContextMenu(container);
				
				// Creates the graph inside the given container
				var graph = new mxGraph(container);

				// Opens a file from the backend
				document.body.appendChild(mxUtils.button('Open', function()
				{
					// Asks for dummy file ID to demonstrate handling of IDs in general
					var id = mxUtils.prompt('Enter file ID');
					
					mxUtils.get('Backend?id=' + encodeURIComponent(id), function(req)
					{
						var node = req.getDocumentElement();
						var dec = new mxCodec(node.ownerDocument);
						dec.decode(node, graph.getModel());
						
						// Stores ID for saving
						graph.id = id;
					});
				}));
				
				// Saves a file to the backend
				document.body.appendChild(mxUtils.button('Save', function()
				{
					if (graph.id == null)
					{
						mxUtils.alert('No open file');
					}
					else
					{
						var enc = new mxCodec();
						var node = enc.encode(graph.getModel());
						var xml = mxUtils.getXml(node);
						
						mxUtils.post('Backend?id=' + encodeURIComponent(graph.id), 'xml=' + encodeURIComponent(xml), function()
						{
							mxUtils.alert('Saved');
						}, function()
						{
							mxUtils.alert('Error');
						});
					}
				}));
			}
		};
	</script>
</head>

<!-- Page passes the container for the graph to the program -->
<body onload="main(document.getElementById('graphContainer'))">

	<!-- Creates a container for the graph with a grid wallpaper -->
	<div id="graphContainer"
		style="position:relative;overflow:hidden;width:321px;height:241px;border:1px solid gray;cursor:default;">
	</div>
</body>
</html>
